package me.leiusual.sharing.ui.store.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.DiffUtil
import me.leiusual.sharing.R
import me.leiusual.sharing.api.model.storeproductquery.Product

/**
 * author: leiusual
 * create on: 2019-07-26
 * description:
 */
class StoreAreaAdapter(
    private val clickListener: (Product) -> Unit
) : PagedListAdapter<Product, StoreAreaViewHolder>(CALLBACK) {

  companion object {
    private val CALLBACK = object : DiffUtil.ItemCallback<Product>() {

      override fun areItemsTheSame(oldItem: Product, newItem: Product): Boolean {
        return oldItem.productId == oldItem.productId
      }

      override fun areContentsTheSame(oldItem: Product, newItem: Product): Boolean {
        return oldItem == newItem
      }
    }
  }

  override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StoreAreaViewHolder {
    val inflater = LayoutInflater.from(parent.context)
    val itemView = inflater.inflate(R.layout.view_holder_store_area, parent, false)
    return StoreAreaViewHolder(itemView, clickListener)
  }

  override fun onBindViewHolder(holder: StoreAreaViewHolder, position: Int) {
    holder.bind(getItem(position) ?: return)
  }
}